#[macro_use]
mod ch_log;
mod ch_regex;

use ch_log::dt_format_with_unix_time as dt_format_with_unix_time;
use ch_log::unix_time_from_dt as unix_time_from_dt;

fn main() {
    println!("Hello, world!");
    // * 表示下一个按指定的格式输出
    println!("{:.*}", 2, 3.1415926);
    log!("{} + {} = {}", 1, 2, 3);

    let ch = '七';
    println!("ch = {}, is = {}", ch, ch.is_alphabetic());
    println!("ch = {}, is = {}", ch, ch.is_ascii_alphabetic());
    println!("32u8={} , b'9'={}", 32u8.is_ascii_whitespace(), b'9'.is_ascii_digit());
    let ch ='\u{130}';
    println!("ch = {}, is = {}", ch, ch.is_alphabetic());
    let ch2 = ch.to_lowercase();
    ch2.for_each(|c| print!("{}", c));
    println!("");

    let mut also_spaceless = "con".to_string();
    also_spaceless.extend(" tri but ion".split_ascii_whitespace());
    println!("also_spaceless = {}", also_spaceless);

    use std::fmt::Write;

    let mut letter = String::new();
    for ch in 'a'..='z' {
        write!(letter, "{}", ch).unwrap();
    }
    println!("letter = {}", letter);

    let mut letter = String::new();
    writeln!(letter, "Whose {} these are I think I know", "rutabagas").unwrap();
    writeln!(letter, "His house is in the village though;").unwrap();

    println!("letter = {}", letter);

    //  `+` cannot be used to concatenate a `&str` with a `String`
    // let parenthetical = "(" + letter +")";
    let parenthetical = "(".to_string() + &letter + ")";
    println!("parenthetical = {}", parenthetical);

    also_spaceless.drain(3..6);
    println!("also_spaceless = {}", also_spaceless);

    let mut s = "a 七有 colada".to_string();
    // 每一个中文是三个字节
    s.replace_range(2..8,"kahlua");
    println!("s = {}", s);
    let now = dt_format();
    log!("now = {}", now);

    let now =1709255430;
    let now = dt_format_with_unix_time(now);
    println!("now = {}", now);

    let now = 1709169030;
    let now = dt_format_with_unix_time(now);
    println!("now = {}", now);

    let now = 1709082630;
    let now = dt_format_with_unix_time(now);
    println!("now = {}", now);

    let now = 1710320045;
    let now_dt = dt_format_with_unix_time(now);
    println!("now = {}, dt={}", now,now_dt);

    let now = unix_time_from_dt(&now_dt);
    println!("now = {}", now);
    let now_dt = dt_format_with_unix_time(now);
    println!("now = {}, dt={}", now,now_dt);

    let now = "A1710320045";
    println!("now = {}", now.starts_with(char::is_alphabetic));

    let str = "str=1;str2=2;str3=3";
    str.split(|x| x == '=' || x == ';').for_each(|x| println!("x = {}", x));

    // fotmat
    let pi = 3.1415926;
    println!("pi = {:.2}", pi);
    println!("pi = {:.*}", 2, pi);

    println!("name={}", get_name());

    let b = "1.235234E3";
    let b = b.parse::<f64>().unwrap();
    println!("b = {}", b);

    // mode 这种指名的参数，值是需要放在最后的， {2} 表示读取第三个参数
    println!("{mode},{2}, {}, {}", 1, 2, "people",mode="mode");

    println!("is_date = {}", ch_regex::is_date("2021-01-01"));
    println!("is_date = {}", ch_regex::is_date("2021-01-01 12:12:12"));
    println!("is_datetime = {}", ch_regex::is_datetime("2021-01-01 12:12:12"));
    println!("is_datetime = {}", ch_regex::is_datetime("2021-01-01"));
    println!("is_email = {}", ch_regex::is_email("sdfdsf.com"));
    println!("is_phone={}", ch_regex::is_phone("12345678901"));
    println!("is_idcard={}", ch_regex::is_idcard("123456789012345678"));

    println!("e\u{301}e\u{300}e\u{302}e\u{303} éèêẽ");
}


fn get_name() -> String {
    let name = std::env::var("USERNAME").unwrap_or_else(|_| "whoever you are".to_string());
    return name;
}


fn dt_format() -> String {
    use std::time::{UNIX_EPOCH, SystemTime} ;
    let sys_now = SystemTime::now();

    println!("sys_now = {:?}", sys_now);

    let now =sys_now.duration_since(UNIX_EPOCH).unwrap();
    // let now = Duration:;
    let bb = now.as_secs();
    return dt_format_with_unix_time(bb);
}

